package cn.yanceysong.gitstats.model;

import lombok.Data;
import lombok.Builder;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Git统计结果
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class GitStatsResult {
    
    /**
     * 仓库路径
     */
    private String repositoryPath;
    
    /**
     * 统计开始时间
     */
    private LocalDateTime startTime;
    
    /**
     * 统计结束时间
     */
    private LocalDateTime endTime;
    
    /**
     * 总提交数
     */
    private int totalCommits;
    
    /**
     * 总贡献者数
     */
    private int totalContributors;
    
    /**
     * 总代码行数
     */
    private int totalLines;
    
    /**
     * 总新增行数
     */
    private int totalAddedLines;
    
    /**
     * 总删除行数
     */
    private int totalDeletedLines;
    
    /**
     * 总修改行数
     */
    private int totalModifiedLines;
    
    /**
     * 贡献者统计列表
     */
    private List<ContributorStats> contributors;
    
    /**
     * 按贡献者邮箱索引的统计信息
     */
    private Map<String, ContributorStats> contributorMap;
    
    /**
     * 添加贡献者统计
     */
    public void addContributor(ContributorStats contributor) {
        if (contributors == null) {
            contributors = new ArrayList<>();
        }
        if (contributorMap == null) {
            contributorMap = new HashMap<>();
        }
        
        contributors.add(contributor);
        contributorMap.put(contributor.getEmail(), contributor);
        
        // 更新总体统计
        this.totalCommits += contributor.getCommitCount();
        this.totalAddedLines += contributor.getAddedLines();
        this.totalDeletedLines += contributor.getDeletedLines();
        this.totalModifiedLines += contributor.getModifiedLines();
        this.totalLines = this.totalAddedLines + this.totalModifiedLines;
        this.totalContributors = contributors.size();
    }
    
    /**
     * 根据邮箱获取贡献者统计
     */
    public ContributorStats getContributorByEmail(String email) {
        return contributorMap != null ? contributorMap.get(email) : null;
    }
}
